package com.liveinstars.api.dto;

import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;

import java.util.stream.Stream;

/**
 * Created by xiongruntu on 2019/1/23.
 */
public class UnifyAppUpdatepwdReq {

    private Integer uid;
    private String mobile;
    private String username;
    private String password;
    private String code;
    private String newpassword;
    /**
     * 1.用户名密码的方式2.手机号验证码方式
     */
    private String updateType;

    public UnifyAppUpdatepwdReq() {

    }

    public UnifyAppUpdatepwdReq(Integer uid, String mobile, String username, String password, String code, String newpassword) {
        this.uid = uid;
        this.mobile = mobile;
        this.username = username;
        this.password = password;
        this.code = code;
        this.newpassword = newpassword;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getNewpassword() {
        return newpassword;
    }

    public void setNewpassword(String newpassword) {
        this.newpassword = newpassword;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUpdateType() {
        return updateType;
    }

    public void setUpdateType(String updateType) {
        this.updateType = updateType;
    }

    public void checkRequiredParam() {
        if (StringUtils.isEmpty(updateType) || !("1".equals(updateType) || "2".equals(updateType))) {
            throw SystemExceptionEnum.ILLEGAL_REQUEST_PARAM.ex;
        }
        if ("1".equals(updateType) && checkUpdatePwdByUserNameParamOk()) {
            throw SystemExceptionEnum.ILLEGAL_REQUEST_PARAM.ex;
        }
        if ("2".equals(updateType) && checkUpdatePwdByMobileParamOk()) {
            throw SystemExceptionEnum.ILLEGAL_REQUEST_PARAM.ex;
        }
    }

    public boolean checkUpdatePwdByMobileParamOk() {
        return Stream.of(mobile, code, newpassword).allMatch(str -> StringUtils.isEmpty(str));
    }

    public boolean checkUpdatePwdByUserNameParamOk() {
        return Stream.of(username, password, newpassword).allMatch(str -> StringUtils.isEmpty(str));
    }

    @Override
    public String toString() {
        return JsonUtil.toJson(this);
    }
}
